// by Xeno
//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_repair\fn_onload.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

disableSerialization;

private ["_display","_vehicle","_unit","_coef","_damage","_damage_val","_estimated_time","_fuel","_rep_count","_type_name","_fuel_str","_dmg_str","_dmg_color","_fuel_color","_estimated"];
DEFAULT_PARAM(_display,0,displayNull);
DEFAULT_PARAM(_unit,1,objNull);
DEFAULT_PARAM(_vehicle,2,objNull);
DEFAULT_PARAM(_can_repair,3,false);

GVAR(Current_Vec) = _vehicle;

_rep_count = switch (true) do {
	case (_vehicle isKindOf "Air"): {GVAR(AirRepairDivider)};
	case (_vehicle isKindOf "Tank" || _vehicle isKindOf "Tracked_APC"): {GVAR(TankRepairDivider)};
	default {GVAR(VehicleRepairDivider)};
};

_fuel = fuel _vehicle;
_damage = damage _vehicle;

_damage_val = _damage / _rep_count;
if (GVAR(is_engineer)) then {
	_damage_val = ceil(_damage_val / 3);
};
_coef = (ceil _damage_val) max 1;
_estimated_time = _damage_val * 3;
_estimated = format[localize "STR_TYR_REPAIR_DLG_SECONDS", (round(_estimated_time) max 1)];



_type_name = [typeOf(_vehicle),0] call mso_fnc_GetDisplayName;

_fuel = round(_fuel * 100);
_fuel_str = "";
switch (true) do {
	case (_fuel <= 10): {_fuel_str = localize "STR_TYR_REPAIR_DLG_FUEL_10"; _fuel_color = [0,0,0,1]; };
	case (_fuel <= 30): {_fuel_str = localize "STR_TYR_REPAIR_DLG_FUEL_30"; _fuel_color = [0,0,0,1]; };
	case (_fuel <= 50): {_fuel_str = localize "STR_TYR_REPAIR_DLG_FUEL_50"; _fuel_color = [0,0,0,1]; };
	          default   {_fuel_str = localize "STR_TYR_REPAIR_DLG_FUEL_70"; _fuel_color = [0,0,0,1]; };
};
_damage = round(_damage * 100);
_dmg_str = "";
_dmg_color = [];
switch (true) do {
	case (_damage >= 70): {_dmg_str = localize "STR_TYR_REPAIR_DLG_DMG_70"; _dmg_color = [0,0,0,1]; };
	case (_damage >= 50): {_dmg_str = localize "STR_TYR_REPAIR_DLG_DMG_50"; _dmg_color = [0,0,0,1]; };
	case (_damage >= 30): {_dmg_str = localize "STR_TYR_REPAIR_DLG_DMG_30"; _dmg_color = [0,0,0,1]; };
	          default     {_dmg_str = localize "STR_TYR_REPAIR_DLG_DMG_10"; _dmg_color = [0,0,0,1]; };
};

// Repair button
_ctrl = _display displayCtrl 1500;
_ctrl ctrlEnable _can_repair;

// Vec Type
_ctrl = _display displayCtrl 1401;
_ctrl ctrlSetText _type_name;
_ctrl ctrlSetTextColor [0.2,0.2,1,1];

// Vec Damage
_ctrl = _display displayCtrl 1402;
_ctrl ctrlSetText _dmg_str;
_ctrl ctrlSetTextColor _dmg_color;

// Vec Can Move
_ctrl = _display displayCtrl 1403;
if (canMove _vehicle) then {
	_ctrl ctrlSetText (localize "STR_TYR_REPAIR_DLG_CANMOVE_ON");
	_ctrl ctrlSetTextColor [0.2,1,0.2,1];
} else {
	_ctrl ctrlSetText (localize "STR_TYR_REPAIR_DLG_CANMOVE_OFF");
	_ctrl ctrlSetTextColor [1,0.2,0.2,1];
};

// Vec Can Fire
_ctrl = _display displayCtrl 1404;
if (canFire _vehicle) then {
	_ctrl ctrlSetText (localize "STR_TYR_REPAIR_DLG_CANFIRE_ON");
	_ctrl ctrlSetTextColor [0.2,1,0.2,1];
} else {
	_ctrl ctrlSetText (localize "STR_TYR_REPAIR_DLG_CANFIRE_OFF");
	_ctrl ctrlSetTextColor [1,0.2,0.2,1];
};

// Vec Fuel
_ctrl = _display displayCtrl 1405;
_ctrl ctrlSetText _fuel_str;
_ctrl ctrlSetTextColor _fuel_color;

// Rep Time
_ctrl = _display displayCtrl 1406;
_ctrl ctrlSetText _estimated;

TRACE("End");
TMFPROFILERSTOP;
